package com.nullam.lorem.contactsync.activities;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.nullam.lorem.contactsync.R;
import com.nullam.lorem.contactsync.adaptor.ListViewAdapter;
import com.nullam.lorem.contactsync.adaptor.NavigationAdapter;
import com.nullam.lorem.contactsync.data.Contact;
import com.nullam.lorem.contactsync.data.ContactReader;
import com.nullam.lorem.contactsync.data.ItemNavigation;

public class MainActivity extends ActionBarActivity implements
		OnSharedPreferenceChangeListener {
	final static private String TAG = "MainAcitivity";
	private RelativeLayout contentLayout;
	private List<Contact> contacts;
	private ListView leftList;
	private ArrayList<ItemNavigation> arrLeft;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// outputText = (TextView) findViewById(R.id.textView1);
		contentLayout = (RelativeLayout) findViewById(R.id.content_main);
		ListView listView = (ListView) findViewById(R.id.listView1);
		leftList = (ListView) findViewById(R.id.left_drawer);
		arrLeft = new ArrayList<ItemNavigation>();

		loadDataToLeftList();
		NavigationAdapter naviAdapter = new NavigationAdapter(this, arrLeft);
		leftList.setAdapter(naviAdapter);

		leftList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v, int pos,
					long id) {
				// TODO Auto-generated method stub
				selectOnLeftMenu(pos);
			}
		});

		ContactReader contactReader = new ContactReader(this);
		contacts = contactReader.fetchContacts();
		ListViewAdapter adapter = new ListViewAdapter(this, contacts);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				OnItemClickListContact(parent, view, position, id);
			}
		});
		listView.setAdapter(adapter);

		Context ctx = getApplicationContext();
		SharedPreferences pre = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		pre.registerOnSharedPreferenceChangeListener(this);
		boolean color = pre.getBoolean("PREFERENCE_CHECKBOX", false);
		if (color) {
			contentLayout.setBackgroundColor(Color.GREEN);
		} else {
			contentLayout.setBackgroundColor(Color.BLUE);
		}
	}

	public void OnItemClickListContact(AdapterView<?> adapter, View view,
			int position, long id) {

		Contact selectedContact = (Contact) adapter.getItemAtPosition(position);
		Toast.makeText(getApplicationContext(),
				"Hehehe : " + selectedContact.toString(), Toast.LENGTH_SHORT)
				.show();
		Intent it = new Intent(this, ContactDetailActivity.class);
		it.putExtra("SELECTED_CONTACT", selectedContact);
		startActivity(it);

	}

	public void loadDataToLeftList() {
		ItemNavigation setting = new ItemNavigation(
				R.drawable.ic_action_settings, "Settings");
		arrLeft.add(setting);

		ItemNavigation rate = new ItemNavigation(
				R.drawable.ic_action_important, "Rate");
		arrLeft.add(rate);

		ItemNavigation about = new ItemNavigation(R.drawable.ic_action_about,
				"About");
		arrLeft.add(about);

		ItemNavigation help = new ItemNavigation(R.drawable.ic_action_help,
				"Help");
		arrLeft.add(help);
	}

	public void selectOnLeftMenu(int pos) {
		switch (pos) {
		case 0: {
			Intent i = new Intent(this, SettingPreferenceActivity.class);
			startActivity(i);
			break;
		}
		case 1: {
			break;
		}
		case 2: {
			Intent i = new Intent(this, AboutActivity.class);
			startActivity(i);
			break;
		}
		case 3: {
			Intent i = new Intent(this, HelpActivity.class);
			startActivity(i);
			break;
		}
		default:
			break;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void onBtnSyncClick(View view) {
		Intent it = new Intent(this, ContactDetailActivity.class);
		startActivity(it);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		Log.e(TAG, "onResume");
		super.onResume();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.e(TAG, "onStart");
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		Log.e(TAG, "onStop");
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			return rootView;
		}
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		// TODO Auto-generated method stub
		boolean color = sharedPreferences.getBoolean("PREFERENCE_CHECKBOX",
				false);
		// Demo setting
		if (color) {
			contentLayout.setBackgroundColor(Color.GREEN);
		} else {
			contentLayout.setBackgroundColor(Color.BLUE);
		}
	}
}
